package com.chuanyang.driverh.mainhome.fragment;

import android.view.View;

import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.adapter.ChooseDriverAdapter;
import com.chuanyang.driverh.responseresult.CarTeamMenmberData;
import com.chuanyang.driverh.responseresult.CarTeamMenmberResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class ChooseDriverFragment extends BaseListFragment {
    private ChooseDriverAdapter listAdapter;
    private ArrayList<CarTeamMenmberData> list;
    public int getDriverId() {
        return listAdapter.getDriverId();
    }
    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new ChooseDriverAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);

    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void initListener() {

    }
    public void resetChoose(){
        onRefresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    public void getData() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", SharedPrefUtil.getFleetId());
        params.put("fleetDriverState", "1");
        MyHttpUtil.selectFleetDriver(activityContext,params, new MyCallback<CarTeamMenmberResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(CarTeamMenmberResult result, int id) {
                if (result!=null && result.getCode() == ConstUtil.OK) {
                    if ( result.getData().size() > 0) {

                        //如果已经分配过，则列表里不显示
//                        removeArrange(list);
                        try {
                            list = result.getData();
                            setDataResult(result.getData());
                        } catch (Exception e) {
                            if (result.getMsg()!=null){
                                PrintUtil.toast(activityContext,result.getMsg());
                            }
                            listAdapter.clear();
                            listAdapter.notifyDataSetChanged();
                        }



                    }
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }
   /* @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        super.onItemClick( parent, view,  position, id);
        CarTeamMenmberData data = list.get(position);
        Intent intent1 = new Intent();
        intent1.putExtra("data", data.getDriverId());
        getActivity().setResult(1, intent1);
        getActivity().finish();
    }*/



    @Override
    public void onGetMore() {
        getData();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }
}
